package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.dto.AcceptanceNodeDTO;
import com.kaizeli.website.pojo.dto.EmployeeAcceptanceDTO;
import com.kaizeli.website.pojo.dto.TravelExpenseDTO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.AcceptanceNodeService;
import com.kaizeli.website.service.EmployeeAcceptanceService;
import com.kaizeli.website.service.TravelExpenseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 验收审核综合控制器
 */
@Tag(name = "验收审核管理", description = "验收审核综合流程相关接口")
@RestController
@RequestMapping("/api/acceptance-review")
public class AcceptanceReviewController {

    @Autowired
    private AcceptanceNodeService acceptanceNodeService;

    @Autowired
    private EmployeeAcceptanceService employeeAcceptanceService;

    @Autowired
    private TravelExpenseService travelExpenseService;

    @Operation(summary = "获取验收节点详细信息（包含人员验收和差旅费用）")
    @GetMapping("/acceptance/{acceptanceId}/detail")
    public Result<Map<String, Object>> getAcceptanceDetail(
            @Parameter(description = "验收节点ID") @PathVariable Long acceptanceId) {
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 获取验收节点信息
            List<AcceptanceNodeDTO> acceptanceNodes = acceptanceNodeService.getAcceptanceNodesByProjectId(acceptanceId).getData();
            if (acceptanceNodes != null && !acceptanceNodes.isEmpty()) {
                result.put("acceptanceNode", acceptanceNodes.get(0));
            }
            
            // 获取人员验收记录
            List<EmployeeAcceptanceDTO> employeeAcceptances = employeeAcceptanceService.getEmployeeAcceptancesByAcceptanceId(acceptanceId).getData();
            result.put("employeeAcceptances", employeeAcceptances);
            
            // 获取差旅费用记录
            List<TravelExpenseDTO> travelExpenses = travelExpenseService.getTravelExpensesByAcceptanceId(acceptanceId).getData();
            result.put("travelExpenses", travelExpenses);
            
            // 计算总费用
            BigDecimal totalLaborCost = employeeAcceptanceService.calculateTotalLaborCost(acceptanceId).getData();
            BigDecimal totalTravelExpense = travelExpenseService.calculateTotalTravelExpense(acceptanceId).getData();
            
            Map<String, BigDecimal> costSummary = new HashMap<>();
            costSummary.put("totalLaborCost", totalLaborCost != null ? totalLaborCost : BigDecimal.ZERO);
            costSummary.put("totalTravelExpense", totalTravelExpense != null ? totalTravelExpense : BigDecimal.ZERO);
            costSummary.put("totalAmount", (totalLaborCost != null ? totalLaborCost : BigDecimal.ZERO)
                    .add(totalTravelExpense != null ? totalTravelExpense : BigDecimal.ZERO));
            
            result.put("costSummary", costSummary);
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "获取验收详情失败：" + e.getMessage());
        }
    }

    @Operation(summary = "提交验收节点进行审核")
    @PostMapping("/acceptance/{acceptanceId}/submit")
    public Result<Void> submitAcceptanceForReview(
            @Parameter(description = "验收节点ID") @PathVariable Long acceptanceId) {
        return acceptanceNodeService.submitAcceptanceNode(acceptanceId);
    }

    @Operation(summary = "审核验收节点")
    @PostMapping("/acceptance/{acceptanceId}/review")
    public Result<Void> reviewAcceptance(
            @Parameter(description = "验收节点ID") @PathVariable Long acceptanceId,
            @Parameter(description = "审核结果") @RequestParam String status,
            @Parameter(description = "审核备注") @RequestParam(required = false) String remark) {
        return acceptanceNodeService.reviewAcceptanceNode(acceptanceId, status, remark);
    }

    @Operation(summary = "完成验收节点")
    @PostMapping("/acceptance/{acceptanceId}/complete")
    public Result<Void> completeAcceptance(
            @Parameter(description = "验收节点ID") @PathVariable Long acceptanceId) {
        return acceptanceNodeService.completeAcceptanceNode(acceptanceId);
    }

    @Operation(summary = "获取项目验收概览")
    @GetMapping("/project/{projectId}/overview")
    public Result<Map<String, Object>> getProjectAcceptanceOverview(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        try {
            Map<String, Object> result = new HashMap<>();
            
            // 获取所有验收节点
            List<AcceptanceNodeDTO> acceptanceNodes = acceptanceNodeService.getAcceptanceNodesByProjectId(projectId).getData();
            result.put("acceptanceNodes", acceptanceNodes);
            
            // 统计各状态数量
            Map<String, Integer> statusCount = new HashMap<>();
            statusCount.put("待提交", 0);
            statusCount.put("待审核", 0);
            statusCount.put("已验收", 0);
            statusCount.put("已完成", 0);
            
            if (acceptanceNodes != null) {
                for (AcceptanceNodeDTO node : acceptanceNodes) {
                    String status = node.getStatus();
                    statusCount.put(status, statusCount.getOrDefault(status, 0) + 1);
                }
            }
            
            result.put("statusCount", statusCount);
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "获取项目验收概览失败：" + e.getMessage());
        }
    }
}
